#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 10000 + 10;
int N, M;
vector<int> nextNodes[maxn];
int inDegree[maxn];
int reward[maxn];

void init(){
	for(int i = 1; i <= N; i ++){
		nextNodes[i].clear();
	}
	memset(inDegree, 0, sizeof(int) * maxn);
	memset(reward, 0, sizeof(int) * maxn);	
}

int solve(){
	queue<int> q;
	for(int i = 1; i <= N; i ++){
		if(inDegree[i] == 0){
			q.push(i);
			reward[i] = 888;
		}
	}
	while(!q.empty()){
		int node = q.front();
		q.pop();
		for(int i = 0; i < nextNodes[node].size(); i ++){
			int nextNode = nextNodes[node][i];
			inDegree[nextNode] --;
			if(inDegree[nextNode] == 0){
				q.push(nextNode);
				reward[nextNode] = reward[node] + 1;
			}
		}		
	}
	int sum = 0;
	for(int i = 1; i <= N; i ++){
		if(reward[i] == 0){
			return -1;
		}
		sum += reward[i];
	}
	return sum;
}

int main(void){
	while(scanf("%d%d", &N, &M) == 2){
		init();
		for(int i = 0; i < M; i ++){
			int a, b;
			scanf("%d%d", &a, &b);
			nextNodes[b].push_back(a);
			inDegree[a] ++;
		}
		printf("%d\n", solve());
	}
	return 0;
}
